var addRoleBtinSubmit=false;
$(function () {
	getList(1);

});
function getList(init){
	if(init==1)$("#baseForm .pageNum").val(1);	
	JY.Model.loading();
	JY.Ajax.doRequest("baseForm",jypath +'/system/role/list',{resourcesCode: resourcesCode, resourcesName: "resName_role_list"},function(data){
		 $("#baseTable tbody").empty();
		 var permitBtn=new Array();
	     //分页对象
    	 var page=data.obj;
    	 var objList = $("#permitBtnList").html();
    	 objList=JSON.parse(objList);
    	 for(ob in objList){
    		 if(objList[ob].permission=='system:role:view'){
    			 permitBtn.push({name:"查看",btnFun:"view",icon:"icon-zoom-in color-purple"});
    		 }else if(objList[ob].permission=='system:role:edit'){
    			 permitBtn.push({name:"修改",btnFun:"edit",icon:"icon-edit color-blue"});
    		 }else if(objList[ob].permission=='system:role:delete'){
    			 permitBtn.push( {name:"删除",btnFun:"del",icon:"icon-remove-sign color-red"});
    		 }else if (objList[ob].permission=="system:role:listAuthorized"){
    			 permitBtn.push({name:"功能授权",btnFun:"authorized",icon:"icon-key color-dark-green"});
    		 }
    	 }
    	
    	 //数据集合
    	 var results=page.rows;
    	 //分页数据
     	 var pageNum=page.pageNumber,pageSize=page.pageSize,totalRecord=page.total;
    	 var html="";
		 if(results!=null&&results.length>0){
    		 var leng=(pageNum-1)*pageSize;//计算序号
    		 for(var i = 0;i<results.length;i++){
        		 var l=results[i];
        		 html+="<tr>";
        		 //html+="<td class='center'><label> <input type='checkbox' name='ids' value='"+l.id+"' class='ace' /> <span class='lbl'></span></label></td>";
        		 html+="<td class='center hidden-480'>"+(i+leng+1)+"</td>";
        		 html+=JY.Tags.setFunction(l.id,permitBtn);
        		 html+="<td class='center'>"+JY.Object.notEmpty(l.roleName)+"</td>";
/*        		 if(l.roleIndexPage=="1"){
        			 html+="<td class='center hidden-480' >首页</td>"; 
        		 }else if(l.roleIndexPage=="2"){
        			 html+="<td class='center hidden-480' >流程管理</td>"; 
        		 }else{
        			 html+="<td class='center hidden-480' >"+l.roleIndexPage+"</td>";  
        		 }*/
        		 
        		 html+="</tr>";		 
        	 } 
    		 $("#baseTable tbody").append(html);
    		 JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getList");
    	 }else{
    		html+="<tr><td colspan='10' class='center'>没有相关数据</td></tr>";
    		$("#baseTable tbody").append(html);
    		$("#pageing ul").empty();//清空分页
    	 }
    	 JY.Model.loadingClose();
	 });
	 JY.Model.loadingClose();
}

function add(){
	clearForm();
	save("新增角色","/system/role/add");
}

function edit(id){
	clearForm();
	JY.Ajax.doRequest(null,jypath+'/system/role/view',{id:id},function(data){
		setForm(data);    
    	save("修改角色","/system/role/edit");  
	});
}

function view(id){
	clearForm();
	JY.Ajax.doRequest(null,jypath+'/system/role/view',{id:id},function(data){
    	setForm(data);    
    	JY.Model.check("roleAddDiv","查看角色");   
	});
}

function del(id){
	JY.Model.confirm("请再次确认是否删除该角色吗？",function(){	
		JY.Ajax.doRequest(null,jypath+'/system/role/delete',{id:id},function(data){
			JY.Model.info(data.resMsg,function(){getList(1);});
		});
	});
}

function save(title,url){
	JY.Model.edit("roleAddDiv",title,function(){
		 if(JY.Validate.form("roleAddForm")){
/*			 var selectType=$("#roleAddForm select[name$='roleIndexPage']").val();
			 if(!JY.Object.notNull(selectType)){
				$("#roleAddForm select[name$='roleIndexPage']").tips({side:1,msg : "请选择！",bg:'#FF2D2D',time:1});
				return;
			 }*/
			 var that =$(this);
			 if(addRoleBtinSubmit){
				 layer.msg("不能重复提交");
				 return;
			 }
			 addRoleBtinSubmit=true;
			 JY.Ajax.doRequest("roleAddForm",jypath +url,null,function(data){	
				 addRoleBtinSubmit=false;
				that.dialog("close"); 
	        	JY.Model.info(data.resMsg,function(){getList(1);});    
			 }); 
		 }	
	});
}
function setForm(data){
	var l=data.obj;
	$("#roleAddForm input[name$='id']").val(JY.Object.notEmpty(l.id));
	$("#roleAddForm input[name$='roleName']").val(JY.Object.notEmpty(l.roleName));
	$("#roleAddForm textarea[name$='remark']").val(JY.Object.notEmpty(l.remark));
	//$("#roleAddForm select[name$='roleIndexPage']").prop("value",JY.Object.notEmpty(l.roleIndexPage));
	$("#roleAddForm select[name$='projectPermission']").prop("value",JY.Object.notEmpty(l.projectPermission));
	$("#roleAddForm select[name$='customerPermission']").prop("value",JY.Object.notEmpty(l.customerPermission));
	
	$("#roleAddForm select[name$='roleType']").prop("value",JY.Object.notEmpty(l.roleType));
}

function clearForm(){
	JY.Tags.cleanForm("roleAddForm");
	//$("#roleAddForm select[name$='roleIndexPage']").prop("value","1");//类别
	//$("#roleAddForm select[name$='roleType']").prop("value","0");//类别
}

function authorized(roleId){
	$("#authorityDiv input[name$='roleId']").val(roleId);
	var authorizedSubmit=false;
	JY.Ajax.doRequest(null,jypath +'/system/role/listAuthorized',{roleId:roleId},function(data){
	         //设置功能权限
	    	 var zNodes=data.obj.functionPoints;
	    	 initFunctionPoints(zNodes);
	         //设置数据权限
	         var dataAuthoritys=data.obj.dataAuthoritys;
	         initdataAuthority(dataAuthoritys);
	         
	         //加载角色权限页面
	         JY.Model.editLargeBig("authorityDiv","角色授权",function(){
		 			if(authorizedSubmit==true){
		 				layer.alert('不能重复提交！');
			            return;
			        }
	        		var zTree = $.fn.zTree.getZTreeObj("authorityTree"),
 					nodes = zTree.getCheckedNodes(),aus ="";
 					for (var i=0, l=nodes.length; i<l; i++) {
 						aus += nodes[i].id + ",";
 					}
 					if (aus.length > 0 ) aus = aus.substring(0, aus.length-1);
 					var that =$(this);
 					authorizedSubmit=true;
 					var dataAuthoritys=getDataAuthoritys();
 					JY.Ajax.doRequest(null,jypath +'/system/role/saveAuthorized',{roleId:roleId,aus:aus,dataAuthoritys:dataAuthoritys},function(data){
 						authorizedSubmit=false;  
 						that.dialog("close");
 	 					JY.Model.info(data.resMsg,function(){getList(1);});			        		 	
 					});
	         });
	});
}

function initFunctionPoints(zNodes){
	//设置
	var setting = {check: {enable: true,chkStyle: "checkbox"/*,chkDisabledInherit: true*/,chkboxType:{"Y":"ps","N":"s"}},data:{simpleData:{enable: true,rootPId:"menu_0000"}}};
    $.fn.zTree.init($("#authorityTree"),setting,zNodes);
    
    $.fn.zTree.getZTreeObj("authorityTree").expandNode();
}


function initdataAuthority(dataAuthoritys){
	if(null==dataAuthoritys)return;
	var html='';
	$.each(dataAuthoritys, function(index, row) {
		html=html+'<tr class="FormData form-inline">';
		html=html+'<td class="CaptionTD" style="text-align:left; padding-left:10px;">'+row.resourcesName+'：</td>';
		html=html+'<td class="DataTD " style=" margin-right:10px; float:right; width:130px;">';
		html=html+'<span>';
		html=html+'<select name="dataAuthoritySelect" class="form-control isSelect105">';
		var selectValue=row.selectValue;
		selectValue=JSON.parse(selectValue);
		$.each(selectValue, function(index, selectValueRowinitdataAuthority) {
			if(row.value==selectValueRow.showValue){
				html=html+'<option selected value="'+row.resourcesCode+':'+selectValueRow.showValue+'">'+selectValueRow.showName+'</option>';
			}else{
				html=html+'<option value="'+row.resourcesCode+':'+selectValueRow.showValue+'">'+selectValueRow.showName+'</option>';
			}
			
		});
		html=html+'</select>';
		html=html+'</span>';
		html=html+'</td>';
		html=html+'</tr>';
	});
    $("#dataAuthorityTable").html("");
    $("#dataAuthorityTable").html(html);
}


function getDataAuthoritys(){
	var dataAuthoritys='';
	$("select[name='dataAuthoritySelect']").each(function(index, row){
		dataAuthoritys += $(this).val() + ",";
	})
	if (dataAuthoritys.length > 0 ) dataAuthoritys = dataAuthoritys.substring(0, dataAuthoritys.length-1);
	return dataAuthoritys;
}





